//
//  HRZAuthorService.m
//  EMarket
//
//  Created by 物联速通信息技术有限公司 on 2018/5/23.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZAuthorService.h"

// Controllers

// Models
#import "HRZUserInfo.h"
#import "HRZAttestationModel.h"
#import "HRZAddressUpdateModel.h"
// Views

// Vendors

// Categories

// Others

@interface HRZAuthorService ()

@end

@implementation HRZAuthorService

+ (void)userRegisterWithPhoneNum:(NSString *)phoneNum
                       verityNum:(NSString *)verityNum
                        password:(NSString *)password
                         success:(void (^)(NSString *))success
                         failure:(void (^)(NSString *))failure {
    NSMutableDictionary *mutDic = [NSMutableDictionary new];
    
    if (phoneNum) {
        [mutDic addEntriesFromDictionary:@{@"phone":phoneNum}];
    }
    if (verityNum) {
        [mutDic addEntriesFromDictionary:@{@"phoneCode":verityNum}];
    }
    if (password) {
        [mutDic addEntriesFromDictionary:@{@"password":password}];
    }
    
    HRZAPIRequest *request = [[HRZAPIRequest alloc] initWithAPIRequestType:kAPIRequestType_Post withSubPath:kRegisterUrl];
    request.params = mutDic;
    [HRZAPIClient execute:request];
    
    request.ServerSuce = ^(NSDictionary *sucObj) {
        success(@"");
    };
    request.ServerFail = ^(NSString *error) {
        failure(error);
    };
}

+ (void)userLoginWithUserAccount:(NSString *)account
                        password:(NSString *)password
                         phoneId:(NSString *)phoneId
                         success:(void (^)(HRZUserInfo *))success
                         failure:(void (^)(NSString *))failure {
    
    NSMutableDictionary *mutDic = [NSMutableDictionary new];
    
    if (account) {
        [mutDic addEntriesFromDictionary:@{@"phone":account}];
    }
    if (password) {
        [mutDic addEntriesFromDictionary:@{@"password":password}];
    }
    
    if (phoneId) {
        [mutDic addEntriesFromDictionary:@{@"phoneId": phoneId}];
    }
    
    HRZAPIRequest *request = [[HRZAPIRequest alloc] initWithAPIRequestType:kAPIRequestType_Get withSubPath:kLoginUrl];
    
    request.params = mutDic;
    
    [HRZAPIClient execute:request];
    
    request.ServerSuce = ^(NSDictionary *sucObj) {
        [HRZUserInfo mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{
                     @"userId":@"id"
                     };
        }];
        HRZUserInfo *userInfo = [HRZUserInfo mj_objectWithKeyValues:sucObj];
        [userInfo saveOrUpdate];
        success(userInfo);
    };
    
    request.ServerFail = ^(NSString *error) {
        
        failure(error);
        
    };
    
}

+ (void)userLogOutWithUser:(HRZUserInfo *)userInfo
                   success:(void (^)(NSString *))success
                   failure:(void (^)(NSString *))failure {
    NSMutableDictionary *mutDic = [NSMutableDictionary new];
    
    
    
    HRZAPIRequest *request = [[HRZAPIRequest alloc] initWithAPIRequestType:kAPIRequestType_Get withSubPath:@""];
    
    request.params = mutDic;
    
    [HRZAPIClient execute:request];
    
    request.ServerSuce = ^(NSDictionary *sucObj) {
        
        success(@"logout_success");
    };
    
    request.ServerFail = ^(NSString *error) {
        
        failure(error);
        
    };
}

+ (void)userShopAttestationWith:(HRZAttestationModel *)model
                        success:(void (^)(NSString *))success
                        failure:(void (^)(NSString *))failure {
    
    NSMutableDictionary *params = [model mj_keyValues];
    HRZAPIRequest *request = [[HRZAPIRequest alloc] initWithAPIRequestType:kAPIRequestType_Post withSubPath:kAttestationUrl];
    request.params = params;
    [HRZAPIClient execute:request];
    
    request.ServerSuce = ^(NSDictionary *sucObj) {
        success(sucObj[@"id"]);
    };
    
    request.ServerFail = ^(NSString *error) {
        failure(error);
    };
}

+ (void)userShopAddressWith:(HRZAddressUpdateModel *)address
                    success:(void (^)(NSString *))success
                    failure:(void (^)(NSString *))failure {
    NSMutableDictionary *params = [address mj_keyValues];
    HRZAPIRequest *request = [[HRZAPIRequest alloc] initWithAPIRequestType:kAPIRequestType_Post withSubPath:kAddressUpdateUrl];
    request.params = params;
    [HRZAPIClient execute:request];
    
    request.ServerSuce = ^(NSDictionary *sucObj) {
        success(@"");
    };
    
    request.ServerFail = ^(NSString *error) {
        failure(error);
    };
}

+ (void)searchShopAddressByShopId:(NSInteger)shopId
                          success:(void (^)(NSString *))success
                          failure:(void (^)(NSString *))failure {
    NSMutableDictionary *params = [NSMutableDictionary new];
    if (shopId) {[params addEntriesFromDictionary:@{@"shopId":@(shopId)}];}
    HRZAPIRequest *request = [[HRZAPIRequest alloc] initWithAPIRequestType:kAPIRequestType_Post withSubPath:kAddressQueryUrl];
    request.params = params;
    [HRZAPIClient execute:request];
    
    request.ServerSuce = ^(NSDictionary *sucObj) {
        success(@"");
    };
    
    request.ServerFail = ^(NSString *error) {
        failure(error);
    };
}

+ (void)userUpdloadImage:(UIImage *)image
                 success:(void (^)(NSString *))success
                 failure:(void (^)(NSString *))failure {
    HRZAPIRequest *request = [[HRZAPIRequest alloc] initWithAPIRequestType:kAPIRequestType_Upload withSubPath:kFileUploadUrl];
    NSMutableDictionary *mutDic = [[NSMutableDictionary alloc] init];
    [mutDic addEntriesFromDictionary:@{@"file":@"image.png"}];

    request.params = mutDic;
    request.images = [NSMutableArray arrayWithObject:image];
    [HRZAPIClient execute:request];
    
    request.ServerSuce = ^(NSDictionary *sucObj) {
        NSLog(@"%@",sucObj);
        success(@"");
    };
    
    request.ServerFail = ^(NSString *error) {
        failure(error);
    };
}

@end
